﻿
$("form").live("submit", function () {
    var onSuccess;
    var form = $(this);
    if (typeof onFormsPostSuccess == 'undefined')
        onSuccess = $(form).attr("onSuccess");
    else
        onSuccess = onFormsPostSuccess;

    var path = form.attr("action");

    var fileInputs = $('input:file', this).length > 0;
    if (fileInputs) {
        var fileInput = $('input:file', this)[0];
        var id = 'jqFormIO' + (new Date().getTime()), fn = '_' + id;
        var $io = $('<iframe id="' + id + '" name="' + id + '" />');
        $io.css({ position: 'absolute', top: '-100000px', left: '-100000px', display: 'none' });

        $(form).attr('target', id);
        $(form).attr("method", "POST");

        $io.appendTo('body');
        var io = $io[0];

        $("#" + id).load(function () {

            $.event.trigger("ajaxStop");
            var doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document;
            var data = $(doc).find('body').find('pre').html();
            handleInputErrors(data, onSuccess);
            $(this).remove();
        });


        $.event.trigger("ajaxStart");

        var f = form[0];
        f.submit();

        return false;
    }

    form = form.serialize();

    $.ajax({
        type: "POST",
        url: path,
        data: form,
        async: false,
        success: function (data) {
            if (data != null)
                if (data.length != 0) {
                    handleInputErrors(data, onSuccess);

                }
                else {
                    $("#divDialog").remove();
                    executeStiring(onSuccess);
                   
                }
        }
    });

    return false;

});

function executeStiring(str) {
    if (typeof str == 'undefined')
        return true;

    var dj_global = this; // global scope reference
    if (window.execScript) {
        window.execScript(str); // eval in global scope for IE            
        return null; // execScript doesn’t return anything
    }
    if (dj_global.eval)
        dj_global.eval(str);
    else
        eval(str);

    return true;
}

function handleInputErrors(data, onSuccess) {
    data = $.parseJSON(data);
    if (data == null)
        executeStiring(onSuccess);

    if (data.length == 0) {
        $("#divDialog").remove();
        executeStiring(onSuccess);
        return false;
    }
    $(".notValdigInput").removeClass("notValidInput");
    $(".validationErrorBox").remove();
    $(data).each(function() {
        $("#" + this.FieldName).addClass("notValidInput");
        $("<span class='validationErrorBox'> * " + this.Message + "</span>").insertAfter("#" + this.FieldName);
    });

    return true;
}


function deleteEntity(title, callback, a) {
    if (!confirm(title)) {
        return false;
    }

    if (arguments.length == 2) {
        callback();
    } else if (arguments.length == 3) {
        var path = $(a).attr("href");
        $.ajax({
                type: "POST",
                url: path,
                success: function() {
                    callback(a);
                }
            });
    }
}